﻿layui.use(['form', 'layer', 'jquery', 'laypage'], function () {
    var form = layui.form,
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery,
		pageData = { "PageSize": 2, "PageCount": 0, "PageNumber": 0, "RowCount": 0 };
    var Aid = 0;

    //表格数据和分页
    function getList(that) {
      
        $.ajax({
            type: "POST",
            url: "Recruitment/Recruitment",
            data: that,
            dataType: "json",
            success: function (data) {
                var result = JSON.parse(data.Data);
                var usersData = result.DataList;
                pageData.PageCount = result.PageCount;
                pageData.PageNumber = result.PageNumber;
                pageData.PageSize = result.PageSize;
                pageData.RowCount = result.RowCount;
                //执行加载数据的方法
                renderDate(usersData);
                showPage(pageData);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                layer.alert(textStatus);
            }
        });
        //渲染数据
        function renderDate(list) {
            var dataHtml = '';
            if (list != null) {
                layui.each(list, function (index, item) {
                    var button = '';
                    if (item.ReitRemark == null) {
                        item.ReitRemark = '';
                    }
                    if (item.ReitState == 0) {
                        item.ReitState = '未通过';
                    } else if (item.ReitState == 1) {
                        item.ReitState = '已通过';
                    } else if (item.ReitState == 2) {
                        item.ReitState = '未审批';
                        button = '<a class="layui-btn" data-method="setTop"  name="Yes">批准</a>';
                    }
                    var plantype = "";
                    dataHtml += '<tr id=>'
                    + '<td>' + item.ReitID + '</td>'
                    + '<td>' + item.Name + '</td>'
                    + '<td>' + item.PostName + '</td>'
                    + '<td>' + item.ReitAmount + '</td>'
                    + '<td>' + item.ReportTime + '</td>'
                    + '<td>' + item.Require + '</td>'
                    + '<td>' + item.Obligation + '</td>'
                    + '<td>' + item.ReitRemark + '</td>'
                     + '<td>' + item.UserName + '</td>'
                    + '<td>' + item.CreateTime + '</td>'
                    + '<td>' + item.ReitState + '</td>'
                    + '<td>'
                    + button
                    + '</td>'
                    + '</tr>';
                });
            } else {
                dataHtml = '<tr><td colspan="10">暂无数据</td></tr>';
            }
            $(".news_content").html(dataHtml);   //渲染数据
            $('.news_list thead input[type="checkbox"]').prop("checked", false);
            form.render(); //渲染表单
        }

    }
    //分页
    function showPage(page) {
        laypage.render({
            elem: 'page',
            count: page.RowCount,//得到总页数
            limit: page.PageSize,
            curr: page.PageNumber + 1,
            groups: 10, //连续显示分页数
            jump: function (obj, first) {
                if (!first) {//防止无限刷新
                    page.PageNumber = obj.curr - 1;
                    getList(page);
                }
            }
        });
    }






    var active = {
        setTop: function () {
            var that = this;
            //多窗口模式，层叠置顶
            layer.open({
                type: 1,
                title: "招聘请求审批",
                skin: "layui-layer-rim",
                area: ["50%"],
                content: $("#test").html()
            });
            /* 渲染表单 */
            form.render();
        }
    }

    var ApproveManager = {
        ApproveSubmit: function (data) {
            var _this = $(this);
            $.ajax({
                type: "POST",
                url: "/Recruitment/Approve",//发送请求  
                data: data.field,
                dataType: "json",
                success: function (result) {
                    if (result.state == 0)
                        layer.alert(result.Message, { icon: 7, time: 1.5 * 1000 });
                    else
                        layer.msg(result.Message, { icon: 1, time: 1.5 * 1000 }, function (index) {
                            layer.closeAll();
                            getList(pageData);
                        });
                }
            });
        }
    }

    $(function () {
        getList(pageData);
        $(document).on("click", ".layui-btn", function () {
            var _this = $(this).parent().parent();
            Aid = _this.children('td').eq(0).text();
            $("#idss").attr("value", Aid);
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
        });

        $(document).on("click", "#Cancel", function () {
            layer.closeAll();
        });
        //监听提交  Loginfrom为lay-filter属性
        form.on('submit(ApproveForm)', function (data) {
            ApproveManager.ApproveSubmit(data);
            return false;
        });
    });
});